<%--
    Document   : search
    Created on : Jul 1, 2011, 9:00:25 AM
    Author     : PhongLK60473
--%>

<%@page import="Object.BookProperties.Type"%>
<%@page import="Object.BookProperties.Author"%>
<%@page import="Object.Page"%>
<%@page import="Object.Order"%>
<%@page import="Object.Book"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%
    
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <style type="" >
            @import url("style/style.css");
            @import url("style/managebooks.style.css");
        </style>
    </head>
    <body>
        <%@include file="header.jsp" %>
        <%
                    if (!user.isAdmin()) {
                        response.sendRedirect("UserController");
                    }
                    int bookid=0;
                    
                    try {
                        bookid = Integer.parseInt(request.getParameter("bookid"));
                    } catch (Exception e){
                        Page.setMessage("Invalid bookid", request);
                        response.sendRedirect("ManagerController");
                    }

                    Book book = new Book(bookid);
                    if(book.getBookTitle()==null){
                        Page.setMessage("Cannot find book", request);
                        response.sendRedirect("ManagerController");
                    }
                    
        %>
        <div id="page">
            <h1>Edit Book</h1>
            <% if(Page.isMessage(request)){ %>
                <div class="message"><%= Page.getMessage(request) %></div>
            <% } %>
            <form action="ManagerController" method="POST">
            <div class="line">
                <div class="label">id</div>
                <input type="text" name="bookid" value="<%= book.getBookId() %>" disabled="disabled" />
                <input type="hidden" name="bookid" value="<%= book.getBookId() %>"/>
            </div>
            <div class="line">
                <div class="label">Title</div>
                <input type="text" name="txtTitle" value="<%= book.getBookTitle() %>" />
            </div>
            <div class="line">
                <div class="label">Description</div>
                <textarea type="text" name="txtDescription"><%= book.getDescription() %></textarea>
            </div>
            <div class="line">
                <div class="label">Author</div>
                <select name="cbAuthor">
                <%
                    Author[] authors = Author.AllAuthors();
                    for(int i=0;i<authors.length;i++){
                        %>
                        <option value="<%= authors[i].getAuthorId() %>"
                        <%= (book.getBookAuthorId()==authors[i].getAuthorId())?"selected":"" %>>
                            <%= authors[i].getAuthorName() %></option>
                        <%
                    }
                %>
                </select>
                <!--
                <input type="text" name="txtAuthor" value="<%= book.getBookAuthor() %>"  />
                -->
            </div>
            <div class="line">
                <div class="label">Type</div>
                <select name="cbType">
                <%
                    Type[] types = Type.AllTypes();
                    for(int i=0;i<types.length;i++){
                        %>
                        <option value="<%= types[i].getTypeId() %>"
                        <%= (book.getBookTypeId()==types[i].getTypeId())?"selected":"" %>>
                            <%= types[i].getTypeName() %></option>
                        <%
                    }
                %>
                </select>
                <!--
                <input type="text" name="txtType" value="<%= book.getBookType() %>"/>
                -->
            </div>
            <div class="line">
                <div class="label">Price</div>
                <input type="text" name="txtPrice" value="<%= book.getPrice() %>"
                       id="price"/>
                <div class="error-left" id="price-error"></div>
            </div>
            <div class="line">
                <div class="label">&nbsp;</div>
                <input type="submit" value="Complete Edit" name="action"/>
            </div>
        </form>
            <script>
                document.onsubmit=function(){
                    var price=document.getElementById("price").value;
                    if(price/1==price){
                        return true;
                    }else{
                        price=document.getElementById("price").focus();
                        document.getElementById("price-error").innerHTML="Your price is invalid";
                        return false;
                    }
                }
            </script>
        </div>
    </body>
</html>
